package com.lxl.workbench.controller;

import com.alibaba.fastjson.JSON;
import com.lxl.commons.contants.BaseServlet;
import com.lxl.commons.contants.Contants;
import com.lxl.commons.domain.ReturnObject;
import com.lxl.commons.utils.UUIDUtils;
import com.lxl.workbench.entity.Category;
import com.lxl.workbench.entity.CategoryStaff;
import com.lxl.workbench.service.BookService;
import com.lxl.workbench.service.CategoryService;
import com.lxl.workbench.service.StaffService;
import com.lxl.workbench.service.impl.BookServiceImpl;
import com.lxl.workbench.service.impl.CategoryServiceImpl;
import com.lxl.workbench.service.impl.StaffServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 北上寻海
 */
@WebServlet({"/selectCategory","/deleteCategory","/addCategory"})
public class CategoryServlet extends BaseServlet {
    //需要使用到的其它对象
    CategoryService categoryService = new CategoryServiceImpl();
    StaffService staffService = new StaffServiceImpl();
    ReturnObject returnObject = new ReturnObject();

    /**
     * 根据条件查询书目
     * @param request 请求域
     * @param response 响应域
     * @throws IOException io异常
     */
    public void selectCategoryFuzzy (HttpServletRequest request, HttpServletResponse response)
            throws IOException {

        //获取前端传送过来的查书目的条件
        String bookCategory = request.getParameter("bookCategory");
        try {
            //先查书目有些什么，再查书目下的书目管理员
            List<Category> categoryList = categoryService.queryBookCategoryFuzzy(bookCategory);
            //要查书目管理员的话，拿category的id去tbl_category_staff
            for (int i = 0;i<categoryList.size();i++){
                    String categoryId = categoryList.get(i).getId();
                    List<CategoryStaff> categoryStaffList = staffService.selectCategoryStaffByManageType(categoryId);
                    //id每查一个，新建一个对应数组
                    String []categoryStaff = new String[categoryStaffList.size()];
                     for(int j=0;j<categoryStaffList.size();j++){
                         categoryStaff[j]=categoryStaffList.get(j).getStaffName();
                     }
                    //一个id查完，将它的书目管理员的数组放进书目列表
                categoryList.get(i).setBookTypeManageStaffs(categoryStaff);
            }

            if(categoryList!=null&&categoryList.size()>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("查询成功");
                returnObject.setRetData(categoryList);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("查询失败");
            }
        }catch (SQLException e){
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage(Contants.RETURN_OBJECT_MESSAGE_FAIL);
        }
        String json =  JSON.toJSONString(returnObject);
        response.getWriter().write(json);
    }

    /**
     * 删除选中的书目，并且把该书目下的所有书籍都删除
     * @param request 请求域
     * @param response 响应域
     * @throws IOException io异常
     */
    public void deleteCategoryByIds (HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        //有斜杠替换掉
        String id = request.getParameter("ids");
        String replaceIds = id.replace("/","");
        String [] delIds =  replaceIds.split(",");
        try {
            //如果抛异常就是删除失败
            categoryService.deleteCategoryByIds(delIds);
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("删除成功");
        } catch (SQLException e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("删除失败");
        }
        String json =  JSON.toJSONString(returnObject);
        response.getWriter().write(json);
    }

    /**
     *馆长添加书目
     * @param request 请求域
     * @param response 响应域
     * @throws IOException io异常
     */
    public void addCategoryByCondition (HttpServletRequest request, HttpServletResponse response)
            throws IOException {
         //把要插入的数据封装好
        Category category = new Category();
        category.setId(UUIDUtils.getUuid());
        category.setBookType(request.getParameter("category"));
        try {
            int result = categoryService.addCategoryByCondition(category);
            if(result>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("添加成功");
                returnObject.setRetData(category);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("添加失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加失败");
        }
        String json =  JSON.toJSONString(returnObject);
        response.getWriter().write(json);
    }


}
